<template>
  <div class="register">
    <div class="nav">
      <router-link :to="{ name: 'User' }">
        <van-icon
          name="arrow-left"
          size="20px"
          color="#000"
          @click-left="onClickLeft"
        />
      </router-link>
      <span @click-right="onClickRight">帮助</span>
    </div>
    <van-form @submit="onSubmit">
      <p>注册小米账号</p>
      <div class="imgs">
        <van-uploader :after-read="afterRead" />
        <img :src="imgUrl" alt="" width="50%" />
      </div>
      <van-field
        v-model="username"
        name="userName"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <van-field
        v-model="nickname"
        name="nickName"
        placeholder="昵称"
        :rules="[{ required: true, message: '请填写昵称' }]"
      />
      <div class="sub">
        <button native-type="submit">注 册</button>
      </div>
      <van-radio-group v-model="checked">
        <van-radio v-model="checked" name="1" checked-color="#fd6c03 ">
          <span style="{display:block;color:grey;}"
            >已阅读并同意<span style="{display:block;color:#fd6c03;}"
              >《用户协议》 《隐私政策》</span
            ></span
          >
        </van-radio>
      </van-radio-group>
    </van-form>
  </div>
</template>

<script>
import { regApi } from "../../api/user";
import { Toast } from "vant";
export default {
  components: {},
  data() {
    return {
      username: "",
      password: "",
      nickname: "",
      imgUrl: "",
      checked: "",
    };
  },
  computed: {},
  watch: {},

  methods: {
    afterRead(file) {
      // 此时可以自行将文件上传至服务器
      console.log(file);
      this.imgUrl = file.content;
    },
    //表单提交
    async onSubmit(values) {
      if (this.checked == true) {
        if (this.imgUrl) {
          //收集数据发送请求
          console.log(this.imgUrl);
          const res = await regApi({ ...values, avatar: this.imgUrl });
          console.log(res);
          if (res.data.code === "success") {
            Toast("注册成功");
            this.$router.push("/login");
          }
        } else {
          Toast.fail("请选择头像");
        }
      } else {
        Toast.fail("请勾选协议");
      }
    },
    onClickLeft() {
      Toast("返回");
    },
    onClickRight() {
      Toast("按钮");
    },
  },
  created() {},
  mounted() {},
};
</script>
<style scoped>
.nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 20px 10px 30px;
}
.van-uploader {
  display: flex;
  justify-content: center;
  margin: 30px 0;
}
.imgs {
  display: flex;
  justify-content: center;
}
.van-field {
  margin: 0 auto;
  width: 90%;
  height: 60px;
  border-radius: 15px;
  background-color: rgb(241, 241, 241);
  color: grey;
  margin-bottom: 15px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
  text-align: center;
  line-height: 30px;
}
.sub {
  text-align: center;
  margin-top: 30px;
}
.sub button {
  width: 90%;
  height: 50px;
  border: 0;
  border-radius: 25px;
  background-color: #fd6c03;
  color: #fff;
  font-size: 20px;
}
p {
  font-weight: 900;
  font-size: 24px;
  margin: 20px;
}
.van-radio-group {
  margin: 20px 0 20px 20px;
  font-size: 12px;
}
</style>
